//提示
function showinteraction(title, icon = 'none') {
	uni.showToast({
		title,
		icon,
		duration: 2000,
		mask: false
	})
}

import Vue from 'vue'

export default (path, data = null, flag = true, loding = true) => {
	let url = '';
	if (flag === true) {
		url = Vue.prototype.$BASE_URL === 'http://jrzg.jibeimall.com/app' ?
			`http://jrzg.jibeimall.com/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=${path}&app=1` :
			`http://admin.huixiangshenghuo.net/app/index.php?i=2&c=entry&m=ewei_shopv2&do=mobile&r=${path}&app=1`
	} else {
		url = path;
	}
	return new Promise((resolve, reject) => {
		if (loding) {
			uni.showLoading({
				title: '数据加载',
				mask: false
			});
		}
		uni.request({
			header: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			method: 'POST',
			url: url,
			data: data,
			success(res) {
				if (loding) {
					uni.hideLoading();
				}
				if (path === 'api.index.new_live_zhibo_start_success') {
					console.log(res);
				}
				switch (true) {
					case res.data.status === -1:
						if (res.data.result.message !== undefined) {
							showinteraction(res.data.result.message);
						}
						resolve(res.data);
						console.log("请求有误", res);

						break;
					case res.data.status === 1:
						if (res.data.message !== undefined) {
							showinteraction(res.data.message);
						} else if (res.data.result.message !== undefined) {
							showinteraction(res.data.result.message);
						}
						resolve(res.data);
						// console.log("请求成功");
						break;
					case res.data.error === 1:
						if (res.data.message !== undefined) {
							showinteraction(res.data.message);
						}
						resolve(res.data);
						console.log("请求有误", res);
						break;
					case res.data.status === 0:
						if (res.data.message !== undefined) {
							showinteraction(res.data.message);
						} else if (res.data.result.message !== undefined) {
							showinteraction(res.data.result.message);
						}
						console.log("请求有误", res);
						resolve(res.data);
						break;
					default:
						if (res.data.message !== undefined) {
							showinteraction(res.data.message);
						}
						// console.log("请求成功");
						resolve(res.data);
				}
			},
			fail(err) {
				uni.hideLoading();
				console.log("请求失败");
				reject(err);
			}
		});
	})
}